package com.czollmann.android.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.czollmann.android.R;
import com.czollmann.android.utils.DateObjectWrapperUtils;

public class BaseActivity extends Activity {
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_refresh :
        DateObjectWrapperUtils.clear();
        Intent refreshIntent = new Intent(this, StartupActivity.class);
        this.startActivity(refreshIntent);
        return true;
      case R.id.menu_settings :
        Intent settingIntent = new Intent(this, SettingsActivity.class);
        this.startActivity(settingIntent);
        return true;
      case R.id.menu_timeline :
        Intent intent = new Intent(this, TimelineActivity.class);
        this.startActivity(intent);
        return true;
      case R.id.menu_exit :
        new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
                                     .setTitle(R.string.quit)
                                     .setMessage(R.string.really_quit)
                                     .setPositiveButton(R.string.yes,
                                                        new DialogInterface.OnClickListener() {
                                                          @Override
                                                          public void onClick(
                                                                    DialogInterface dialog,
                                                                    int which) {
                                                            // Stop the activity
                                                            final Intent i = getIntent();
                                                            setResult(Activity.RESULT_OK, i);
                                                            finish();
                                                          }
                                                        })
                                     .setNegativeButton(R.string.no, null)
                                     .show();
        return true;
      default :
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
